package test.netty;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;

public class Client {
	
	public static void main(String[] args) {
		ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(  
                Executors.newCachedThreadPool(),  
                Executors.newCachedThreadPool()));
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {  
            @Override  
            public ChannelPipeline getPipeline() throws Exception {  
                return Channels.pipeline(new SimpleChannelHandler() {
                	@Override
                	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
                		System.out.println("I'm client");
                		String msg = "I'm client";
                		ChannelBuffer buffer = ChannelBuffers.buffer(msg.length());  
                		buffer.writeBytes(msg.getBytes());  
                		e.getChannel().write(buffer);  
                	}
                });  
            }  
        });
		
		bootstrap.connect(new InetSocketAddress("127.0.0.1", 7878));
	}

}
